﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using log4net;
//[assembly: log4net.Config.DOMConfigurator(ConfigFile = "web.config", Watch = true)]
namespace Tools.Framework
{

    public static class ErrorHelper
    {


        private static string LogPath
        {
            get
            {
                string path = System.Web.HttpContext.Current.Server.MapPath("~/log/");
                DirectoryInfo d = new DirectoryInfo(path);
                if (!d.Exists)
                {
                    d.Create();
                }
                return path + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
            }
        }


        /// <summary>
        /// 错误记录
        /// </summary>
        public static void ErrorLog(this Exception ex, string ip = "")
        {
            Log4NetErrorLog(ex, Log4NetErrorEnum.错误); return;

            //System.IO.File.AppendAllLines(LogPath, new string[]
            //    {
            //        "<Begin>--------------------------------------------------------------------<Begin>",
            //        "请求时间："+DateTime.Now.ToString("yyyy-mm-dd HH:mm:ss"),
            //        "请求路径："+ex.TargetSite,
            //        "请求主机:"+ip,
            //        "日志信息："+ex.Message,
            //        "日志来源:"+ex.Source,
            //        "日志内容:"+(ex.InnerException==null?"":ex.InnerException.Message),
            //        "<End>--------------------------------------------------------------------<End>",
            //        "",
            //        ""
            //    });


        }

        public static void ErrorLog(string msg, string ip = "")
        {
            Log4NetErrorLog(new Exception(msg), Log4NetErrorEnum.一般信息); return;

            //System.IO.File.AppendAllLines(LogPath, new string[]
            //    {
            //        "<Begin>--------------------------------------------------------------------<Begin>",
            //        "请求时间："+DateTime.Now.ToString("yyyy-mm-dd HH:mm:ss"),
            //        "日志内容:"+msg,
            //        "请求主机:"+ip,
            //        "<End>--------------------------------------------------------------------<End>",
            //        "",
            //        ""
            //    });

        }

        public static void Log4NetErrorLog(Exception ex, Log4NetErrorEnum errorEnum)
        {
            ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
            switch (errorEnum)
            {
                case Log4NetErrorEnum.严重错误:
                    //记录严重错误
                    log.Fatal("fatal", ex);
                    break;
                case Log4NetErrorEnum.错误:
                    //记录错误日志
                    log.Error("error", ex);
                    break;
                case Log4NetErrorEnum.一般信息:
                    //记录一般信息
                    log.Info("info", ex);
                    break;
                case Log4NetErrorEnum.调试信息:
                    //记录调试信息
                    log.Debug("debug", ex);
                    break;
                case Log4NetErrorEnum.警告信息:

                    //记录警告信息
                    log.Warn("warn", ex);
                    break;
            }
        }
        public enum Log4NetErrorEnum
        {
            错误 = 1,//error
            严重错误 = 2,//fatal
            一般信息 = 3,//info
            调试信息 = 4,//debug
            警告信息 = 5//warn
        }
    }


}
